using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using FastTests.Voron;
using Sparrow.Server;
using Sparrow.Threading;
using Tests.Infrastructure;
using Voron;
using Voron.Data.Lookups;
using Voron.Data.PostingLists;
using Voron.Util.PFor;
using Xunit;
using Xunit.Abstractions;

namespace FastTests.Corax.Bugs;

public class PostingListSize : StorageTest
{
    public PostingListSize(ITestOutputHelper output) : base(output)
    {
    }

    
    [RavenFact(RavenTestCategory.Voron)]
    public unsafe void CanManagePageSplit2()
    {
        var reader = new StreamReader(typeof(PostingListAddRemoval).Assembly.GetManifestResourceStream("FastTests.Corax.Bugs.page-base64.txt"));
        while (true)
        {
            string line = reader.ReadLine();
            if (line == null) break;
            
            // we are reproducing a scenario here in the worst possible way, but simply *copying* the raw bytes into the page
            // this is done because we aren't sure _how_ we gotten to this state. Note that changing the behavior / structure of lookup may mess up
            // this test, but that is probably not likely, since the format should be backward compatible. 
            byte[] data = Convert.FromBase64String(line);
            long rootPage;
            using( var tx = Env.WriteTransaction())
            {
                var list = tx.LookupFor<Int64LookupKey>("test");
                rootPage = list.State.RootPage;
                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                // raw copying of the data to the page, bypassing the actual logic
                fixed (byte* b = data)
                {
                    Page modifyPage = tx.LowLevelTransaction.ModifyPage(rootPage);
                    Unsafe.CopyBlock(modifyPage.Pointer,b, 8192);
                    modifyPage.PageNumber = rootPage; // we overwrote that
                    tx.Commit();
                }
            }
            
            var parts = reader.ReadLine()!.Split(' ');

            using( var tx = Env.WriteTransaction())
            {
                var list = tx.LookupFor<Int64LookupKey>("test");
                list.Add(long.Parse(parts[0]), long.Parse(parts[1]));
                tx.Commit();
            }
        }
    }

    
    [RavenFact(RavenTestCategory.Voron)]
    public void CanManagePageSplit()
    {
        var reader = new StreamReader(typeof(PostingListAddRemoval).Assembly.GetManifestResourceStream("FastTests.Corax.Bugs.PostListSplit.txt"));
        using var tx = Env.WriteTransaction();
        var list = tx.LookupFor<Int64LookupKey>("test");

        while (true)
        {
            var l = reader.ReadLine();
            if (l == null) break;
            string[] parts = l.Split(' ');
            list.Add(long.Parse(parts[0]), long.Parse(parts[1]));
        }
    }

    [RavenFact(RavenTestCategory.Voron)]
    public void PostingListSizeWouldBeReasonable()
    {
        var vals = new long[]
        {
            96086203228164, 98233082920964, 98233200267268, 98233351409668, 98233409961988, 98233477046276, 98241127481348, 98260245262340, 98298019155972,
            100924106416132
        };
        
        {
            using var tx = Env.WriteTransaction();
            var list = tx.OpenPostingList("test");


            foreach (long v in vals)
            {
                list.Add(v);
            }

            tx.Commit();
        }

        {
            using var tx = Env.ReadTransaction();
            var list = tx.OpenPostingList("test");
            Assert.Equal(1, list.State.LeafPages);
            Assert.Equal(0, list.State.BranchPages);
        }
         
        {
            using var tx = Env.ReadTransaction();
            var list = tx.OpenPostingList("test");
            var actual = new long[PostingListLeafPage.GetNextValidBufferSize(vals.Length)];
            var it = list.Iterate();
            Assert.True(it.Fill(actual, out var total));
            Assert.Equal(total, vals.Length);
            Assert.Equal(vals, actual.Take(total));
            Assert.False(it.Fill(actual, out  total));
        }
    }
    
    [RavenFact(RavenTestCategory.Voron)]
    public unsafe void ShouldNotCrash()
    {
        var buffer = Convert.FromBase64String(
            "BOSzdwAAAAAgBgAA7RVuE///AAAAAAD4LAAArAMAADAFAABAIwAAJAkAACgcAADwHgCYCACqSgdAci0BgPd4AsArDgUA4Z0FALcFBYCZOg4AOQHAewAAML8AAcAZAAFAFgACoCcAAfAZAABQEgADABwAVAoAm2QNAPJACADxkAkA8ZgJAFq4NgBHdAYAEpQHAGEBAIUADADBAAQA9AADwIEAAsCmAAGAsQECgEQBB4BmAAAhAAzQPwDEEBoAvAAZANSAqgAE4CAAtNATAJgQPgD4CwBvAQQAvAAJAEoBBADiASkAHAYMACEDEAAVCx0AAgGAYgDAACYA0EA7AMBAkwOgwCQD8IDUAGCAKAGwwGEBMAUAsCptAEAnHgDcC2QA8A9LAMwTKwBMD/0ACBZHAGghANkGwAC9D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AAAAAAAAAAAQAAAAAAAAD+FAAAAAAAAAAAAQAAAAAAAAD+dAEAAAAAAAAAAAAAAAAAAAD+zAEAAAAAAAAAAAAAAAAAAAD+0AEAAAAAAAAAAAAAAAAAAAAVXSAGBwgJCg0OEBMUFRYYGRobHB4gIyQlJigpKisvMTQ6O0RHSUpPUVhaXGRpbHF0dXl6e31+gIGGiIqPkJSXmJqbnJ+goqmrrLG2v8jLzM7P0NXX2Nrd4eTp8ff5/P/+UAAAAAACAAAAAAAAAAAAAAD+WAAAAAABAAAAAAAAAAAAAAD+XAEAAAAAAAAAAAAAAAAAAAD+aAAAAAAAAAAAAAAAAAEAAAD+wAAAAAAAAAAAAAAAAAEAAAD+yAAAAAAAAAAAAAAAAAEAAAD+9AAAAAAAAAAAAQAAAAAAAAAVUyABAgUICQoLDhATFRgcICkxOzxCR0pLTFFSVFlaXF1gY2VnaGprbXOCi46PkJGWl5ifp6uvsbi5vb7AwsPGx8jJysvR2Nrc3d7g4eLl5uvt7vX2+f48AAAAAAEAAAAAAAAAAAAAAP6QAAAAAAEAAAAAAAAAAAAAAP7EAAAAAAAAAAAAAAAAAQAAAP7sAAAAAAAAAAACAAAAAAAAABohIAkNOzw9PkBBQ0VZW1xeYGl7fIKJj5CRvr/Ax83Q5uru9P4MAAAAAAAAAAAAAAAAAQAAAP4QAAAAAAAAAAAAAAAAAQAAAP5AAAAAAAAAAAAAAAAAAgAAAP5YAQAAAAAAAAAAAAAAAAAAAP54AAAAAAAAAAABAAAAAAAAAP6kAAAAAAAAAAABAAAAAAAAAP7EAAAAAAEAAAAAAAAAAAAAAP78AQAAAAAAAAAAAAAAAAAAABVWIAYMDQ8QEhMbIiMkJigpKywxNzw9QkNHSFBTWFlaXmBhYmNrbXJzdnd6fX6DhY+VmJucpKWpqqusrq+xtLa3uLm6vsHCw8XJ09vf5u/y9PX29/n8/f7//84=");

        using var bsc = new ByteStringContext(SharedMultipleUseFlag.None);
        // we need to setup the allocation pattern in a very specific manner to trigger this
        using var aa = bsc.Allocate(4000, out _);
        using var ab = bsc.Allocate(4000, out _);
        using var ac = bsc.Allocate(4000, out _);
        
        // so the small entry for the native list will come before the earlier allocation of the buffer
        var small = bsc.Allocate(128, out _);
        var big = bsc.Allocate(4096, out _);
        small.Dispose();
        big.Dispose();

        var output = stackalloc long[2048];
        fixed (byte* input = buffer)
        {
            int existingCount = 0;
            var reader = new FastPForBufferedReader(bsc, input, buffer.Length);
            while (true)
            {
                var read = reader.Fill(output + existingCount, 2048 - existingCount);
                existingCount += read;
                if (read == 0)
                {
                    reader.Dispose();
                    break;
                }
            }
        }
    }
}
